package com.augurit.gzcgwsp.csshlj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.csshlj.dao.CcljclcDao;
import com.augurit.gzcgwsp.csshlj.entity.Ccljclc;
import com.augurit.gzcgwsp.csshlj.service.ICcljclcService;
import com.augurit.gzcgwsp.csshlj.web.form.CcljclcForm;



/*
 * 城市生活垃圾处置厂service实现类
 */
@Service
@Transactional
public class CcljclcServiceImpl  implements ICcljclcService{
	@Autowired
	private CcljclcDao ccljclcDao;
	
	/**
	 * 获取form
	 */
	public CcljclcForm getForm(Long clcId) {
		Ccljclc entity=ccljclcDao.get(clcId);
		CcljclcForm form=new DozerBeanMapper().map(entity,CcljclcForm.class);
		return form;
	}

	public List<CcljclcForm> getListByCzId(Long czId) {
		List<Ccljclc> list=this.getListByCzid(czId);
		List<CcljclcForm> forms=new ArrayList<CcljclcForm>();
		for(Ccljclc entity:list){
			CcljclcForm form=new DozerBeanMapper().map(entity,CcljclcForm.class);
			forms.add(form);
		}
		return forms;
	}

	/**
	 * 根据ywid查询数据
	 * @param czId
	 * @return
	 */
	private List<Ccljclc> getListByCzid(Long czId){
		String sql=" from "+Ccljclc.class.getName()+" where  czid="+czId;
		List<Ccljclc> list=this.ccljclcDao.find(sql);
		return list;
	}


	public void SaveOrUpdate(CcljclcForm form) {
		Ccljclc entity=new DozerBeanMapper().map(form,Ccljclc.class);
		this.ccljclcDao.save(entity);
	}

	public void parseYwCcljclc(HSSFSheet hssfsheet,Long qybh,Long ywid){
		String hql = "from Ccljclc t where t.czid = '"+ywid+"'";
		List<Ccljclc> list = new ArrayList<Ccljclc>();
		list = this.ccljclcDao.find(hql, list);
		for(int i=0; i<list.size();i++){
			this.ccljclcDao.delete(list.get(i));
		}
		try {
			String address ="";
			String clnl="";//处理能力
			HSSFRow hssfrow = hssfsheet.getRow(1);
			if (hssfrow != null) {
				if (hssfrow.getCell((short) 1) == null) {
					address = "";
				} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
					address = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
				} else {// 如果EXCEL表格中的数据类型为字符串型
					address = hssfrow.getCell((short) 1).getStringCellValue().trim();
				}
			}
			HSSFRow hssfrow2 = hssfsheet.getRow(2);
			if (hssfrow2 != null) {
				if (hssfrow2.getCell((short) 1) == null) {
					clnl = "";
				} else if (hssfrow2.getCell((short) 1).getCellType() == 0) {
					clnl = new Double(hssfrow2.getCell((short) 1).getNumericCellValue()).toString();
				} else {// 如果EXCEL表格中的数据类型为字符串型
					clnl = hssfrow2.getCell((short) 1).getStringCellValue().trim();
				}
			}
			if(!StringUtils.isEmpty(address)){
				Ccljclc Ccljclc = new Ccljclc();
				Ccljclc.setAddress(address);
				Ccljclc.setQyid(qybh);
				Ccljclc.setCzid(ywid);
				Ccljclc.setClnl(clnl);
				this.ccljclcDao.save(Ccljclc);
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	
}
